package com.creating.mict.action;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.creating.mict.domain.Role;
import com.creating.mict.service.IRoleService;
import com.opensymphony.xwork2.ActionSupport;

/** 
 * @ClassName: RoleAction 
 * @Description: 角色Action
 * @author xuelei
 * @date 2012-3-9 下午8:56:11 
 *  
 */
@SuppressWarnings("serial")
public class RoleAction extends ActionSupport {

	@Resource
	private IRoleService roleService;
	
	/**
	* @Title: findRoles 
	* @Description: 查询当前用户所拥有的角色 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String queryRoleList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Role> list = roleService.queryRoleList();
		if(list == null) {
			total = 0;
		} else {
			total = list.size();
		}
		StringBuffer jsonStr = new StringBuffer("[");
		
		for (int i = 0; i < total; i++) {
			Role role = (Role) list.get(i);
			jsonStr.append("{");
			jsonStr.append("fid:'").append(role.getFid()).append("',");
			jsonStr.append("fname:'").append(role.getFname()).append("',");
			jsonStr.append("fcdate:'").append(role.getFcdateStr()).append("',");
			jsonStr.append("fdesc:'").append(role.getFdesc()).append("'");
			if (i == total - 1) {
				jsonStr.append("}");
			} else {
				jsonStr.append("},");
			}
		}
		jsonStr.append("]");
		String str = "{totalProperty: " + total + ", root:"
				+ jsonStr.toString() + "}";
		request.setAttribute("result", str);
		return "success";
	}
	
	/**
	* @Title: addRole 
	* @Description: 新增角色
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String addRole() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//当前登录用户名
		String loginName = "admin";//(String)request.getSession().getAttribute("loginName");
		Role role = new Role();
		//修改
		if(fid != null && !fid.equals("")) {
			role = roleService.findRoleById(fid);
			role.setFname(fname);
			role.setFdesc(fdesc);
		//新增	
		} else {
			//随机数生成id
			int num = (int)(Math.random()*1000000);
			role.setFid("" + num);
			role.setFname(fname);
			role.setFdesc(fdesc);
			role.setFcdate(new Date());
			role.setFcuser(loginName);
			role.setFtype("2");//普通角色
		}
		
		if(roleService.saveRole(role)) {
			request.setAttribute("result", "{success:true,message:'ok'}");
		} else {
			request.setAttribute("result", "{success:false,message:'error'}");
		}
		return "success";
	}
	
	/**
	* @Title: deleteRole 
	* @Description: 根据删除单个角色
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String deleteRoleById() {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(roleService.deleteRoleById(fid)) {
			request.setAttribute("result", "{success:true,message:'ok'}");
		}else {  //失败
			request.setAttribute("result", "{success:false,message:'失败！'}");	
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: saveRoleColumns 
	* @Description: 保存角色对应栏目
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String saveRoleColumns() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo = roleService.saveRoleColumns(fid, ids);
		if(boo) {
			request.setAttribute("result", "{success:true,message:'保存成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'保存失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: clearRoleColumns 
	* @Description: 清空角色对应栏目
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String clearRoleColumns() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo = roleService.clearRoleColumns(fid);
		if(boo) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	
	private int limit = 30;
	private int start = 0;
	private int total;
	
	private String fid;
	private String fname;
	private String fdesc;
	
	private String ids;

	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * @param limit the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * @return the start
	 */
	public int getStart() {
		return start;
	}

	/**
	 * @param start the start to set
	 */
	public void setStart(int start) {
		this.start = start;
	}

	/**
	 * @return the total
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(int total) {
		this.total = total;
	}

	/**
	 * @return the fid
	 */
	public String getFid() {
		return fid;
	}

	/**
	 * @param fid the fid to set
	 */
	public void setFid(String fid) {
		this.fid = fid;
	}

	/**
	 * @return the fname
	 */
	public String getFname() {
		return fname;
	}

	/**
	 * @param fname the fname to set
	 */
	public void setFname(String fname) {
		this.fname = fname;
	}

	/**
	 * @return the fdesc
	 */
	public String getFdesc() {
		return fdesc;
	}

	/**
	 * @param fdesc the fdesc to set
	 */
	public void setFdesc(String fdesc) {
		this.fdesc = fdesc;
	}

	/**
	 * @return the ids
	 */
	public String getIds() {
		return ids;
	}

	/**
	 * @param ids the ids to set
	 */
	public void setIds(String ids) {
		this.ids = ids;
	}

}
